package com.lmpop.frame.bpm;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.List;

import com.lmpop.frame.SysMap;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.dao.IbatisDbmImpl;

public class BpmImpl
  implements BpmInter
{
  private String result_code = "1";
  private String after_code = "0";

  public SysMap bpmMgr(SysMap sysMapPara)
  {
    SysMap sysMap = new SysMap();
    sysMap = sysMapPara;
    sysMap = bpmExe(sysMap, "befo", "1");

    sysMap = bpmExe(sysMap, sysMap.getString("bpm_id"), "0");

    sysMap = bpmExe(sysMap, "afte", "1");

    if (this.after_code.equals("1")) sysMap.setString("result_code", "1");

    return sysMap;
  }

  public SysMap bpmExe(SysMap sysMapPara, String bpm_id, String type)
  {
    DbmInterf dbmMgr = new IbatisDbmImpl();
    SysMap sysMap = new SysMap();
    sysMap = sysMapPara;
    BpmInfo bpmInfo = new BpmInfo();
    List bpmList = bpmInfo.getBpmListByBpmId(bpm_id);

    System.out.println("bpmList*****************" + bpmList + "*****type***" + type + "**bpm_id****" + bpm_id);
    dbmMgr.start();
    sysMap = invokeMethodList(bpmList, sysMap, dbmMgr);
    if (this.result_code.equals("0")) dbmMgr.commit();
    dbmMgr.end();

    if (type.equals("0")) {
      System.out.println("====================Start Public bpm====================");
      List pubList = bpmInfo.getListByPk("publ");

      dbmMgr.start();
      System.out.println("pubList___test*********************" + pubList);
      sysMap = invokeMethodList(pubList, sysMap, dbmMgr);
      if (this.result_code.equals("0")) dbmMgr.commit();
      dbmMgr.end();

      System.out.println("====================End Public bpm====================");

      String map_bpm_id = sysMap.getString("bpm_id");
      if (map_bpm_id.equals("")) {
        sysMap.setString("result_info", "bpm_id in request is null");
      }
      if ((!map_bpm_id.equals("")) && (bpm_id.equals(""))) {
        sysMap.setString("result_info", "ts_radetype trade_type_code is null");
      }
      if ((!map_bpm_id.equals("")) && (!bpm_id.equals("")) && ((bpmList == null) || (bpmList.size() == 0))) {
        sysMap.setString("result_info", "ts_bpmdefinition bpm_id is null");
      }
      sysMap.setString("result_code", this.result_code);
    }

    return sysMap;
  }

  private SysMap invokeMethodList(List bpmList, SysMap sysMap, DbmInterf dbmMgr)
  {
    try {
      if ((bpmList != null) && (bpmList.size() > 0)) {
        Hashtable bpmMap = new Hashtable();
        for (int i = 0; i < bpmList.size(); i++) {
          String node_class = ""; 
          String node_method = "";
          bpmMap = (Hashtable)bpmList.get(i);
          if (bpmMap.get("node_class") != null) node_class = bpmMap.get("node_class").toString();
          if (bpmMap.get("node_method") != null) node_method = bpmMap.get("node_method").toString();

          System.out.println("/////////////////////Start invoke method [" + node_class + "." + node_method + "]/////////////////////");

          Class bpmClass = Class.forName(node_class.trim());
          Object objBpm = null;
          try {
            objBpm = bpmClass.newInstance();
            Method bpmMethod = objBpm.getClass().getDeclaredMethod(node_method.trim(), new Class[] { DbmInterf.class, SysMap.class });
            sysMap = (SysMap)bpmMethod.invoke(objBpm, new Object[] { dbmMgr, sysMap });
          } catch (InstantiationException e) {
            e.printStackTrace();
            sysMap.setString("result_code", "1");
          } catch (IllegalAccessException e) {
            e.printStackTrace();
            sysMap.setString("result_code", "1");
          } catch (IllegalArgumentException e) {
            e.printStackTrace();
            sysMap.setString("result_code", "1");
          } catch (InvocationTargetException e) {
            e.printStackTrace();
            sysMap.setString("result_code", "1");
          }
          this.result_code = sysMap.getString("result_code");
          if (this.result_code.equals("1")) this.after_code = this.result_code;

          System.out.println("All trade field : " + sysMap.list());
          System.out.println("/////////////////////End invoke method [" + node_class + "." + node_method + "]/////////////////////");

          if (this.result_code.equals("1"))
            break;
        }
      }
    }
    catch (ClassNotFoundException e) {
      throw new RuntimeException("invoke class can't find :" + e);
    }
    catch (NoSuchMethodException e) {
      throw new RuntimeException("invoke method can't find :" + e);
    }

    return sysMap;
  }

  public Hashtable getTradetypeMap(String trade_type_code) {
    TradetypeInfo tradetypeInfo = new TradetypeInfo();
    List list = tradetypeInfo.getListByPk(trade_type_code);
    Hashtable tMap = new Hashtable();
    if ((list != null) && (list.size() > 0))
      tMap = (Hashtable)list.get(0);
    else {
      tMap = null;
    }
    return tMap;
  }
}